
#include "userFuncDataFlash.h"


#include "pfdl.h"
#include "pfdl_types.h"
#include "iodefine.h"
#include "r_cg_macrodriver.h"
#include "r_cg_wdt.h"


//
uint8_t WriteDataflash( uint8_t * buffer, uint32_t Addr, uint16_t lenth )
{
    pfdl_status_t ret;
    pfdl_descriptor_t descriptor;
    pfdl_request_t requester;

    /* ---- Setting for PFDL_Open ---- */
    descriptor.fx_MHz_u08 = FDL_FRQ;
    descriptor.wide_voltage_mode_u08 = FDL_VOL;
    PFDL_Open( &descriptor );

    /* ---- Setting for blank check ---- */
    requester.command_enu = PFDL_CMD_BLANKCHECK_BYTES;
    requester.index_u16 = ( Addr - 0x0F1000 ); // g_write_address;0x0F1000 according to MCU
    requester.bytecount_u16 = lenth;
    ret     = PFDL_Execute( &requester ); /* Blank check */

    /* ---- Waiting for command finish ---- */
    while ( ret == PFDL_BUSY )
    {
        ret     = PFDL_Handler();       /* Status check process */
    }

    /* **** When blank check error **** */
    if ( ret == PFDL_ERR_MARGIN )
    {
        /* ---- Setting for erase ---- */
        requester.command_enu = PFDL_CMD_ERASE_BLOCK;
        requester.index_u16 = ( Addr - 0x0F1000 ) / 0x400; // TARGET_BLOCK;
        ret     = PFDL_Execute( &requester ); /* Erase block data */

        /* ---- Waiting for command finish ---- */
        while ( ret == PFDL_BUSY )
        {
            ret     = PFDL_Handler();   /* Status check process */
        } /* Erase data of target block */
    }

    /* **** When other than blank check error **** */
    else 
    {
        /* Do nothing */
    }

    if ( ret == PFDL_OK )
    {
        /* ---- Setting for write ---- */
        requester.command_enu = PFDL_CMD_WRITE_BYTES;
        requester.index_u16 = ( Addr - 0x0F1000 ); // g_write_address;;
        requester.bytecount_u16 = lenth;
        requester.data_pu08 = buffer;   //&g_write_value;
        ret     = PFDL_Execute( &requester ); /* Execute write */

        /* ---- Waiting for command finish ---- */
        while ( ret == PFDL_BUSY )
        {
            ret     = PFDL_Handler();   /* Status check process */

        } /* Write data process */
        /* ==== When write data is success ==== */

        if ( ret == PFDL_OK )
        {
            /* ---- Setting for verify ---- */
            requester.command_enu = PFDL_CMD_IVERIFY_BYTES;
            requester.index_u16 = ( Addr - 0x0F1000 ); // g_write_address;;
            requester.bytecount_u16 = lenth;
            ret     = PFDL_Execute( &requester ); /* Execute internal verify */

            /* ---- Waiting for command finish ---- */
            while ( ret == PFDL_BUSY )
            {
                ret     = PFDL_Handler(); /* Status check process */
            }
        }

        /* ==== When writing data is failure ==== */
        else 
        {
            ret     = ( pfdl_status_t ) ( PFDL_NG );
        }
    }

    /* **** When blank check or data erase is failure **** */
    else 
    {
        ret     = ( pfdl_status_t ) ( PFDL_NG );
    }

    /* Reset WDT */
    R_WDT_Restart();
    PFDL_Close();                       /* Close FDL */
    return ret;
}


void ReadFlashData( uint8_t * buffer, uint32_t Addr, uint16_t lenth )
{
    uint16_t temp;
    volatile uint32_t w_count;
    uint8_t * snnumber;

    snnumber = ( Addr );

    /**** Only dataflash needed start*****/
    /* Some chips do not have the DFLEN register, so you do not need to set it at this time. */
    if ( 0 == DFLEN )
    {
        DFLEN   = 1U;
        for ( w_count = 0U; w_count < 3U; w_count++ )
        {
            NOP();
        }
    }

    /**** Only dataflash needed end******/
    for ( temp = 0; temp < lenth; temp++ )
    {
        *buffer = *snnumber;
        buffer++;
        snnumber++;
    }
}


